#pragma once

#include <iostream>
#include <sys/epoll.h>
#include "Log.hpp"

using namespace LogModule;
// 封装epoll模型
namespace EpollerModule
{
	class Epoller
	{
	public:
		Epoller()
		{}
		~Epoller()
		{}
		void Create() // 创建epoll模型
		{
			_epfd = epoll_create(256);
			if(_epfd < 0)
			{
				std::cout << "epoll_create error" << std::endl;

				return;
			}
			std::cout << "epoll_create success" << std::endl;
		}
		void Ctl(int sockfd,uint32_t events,int flag) // 控制epoll模型
		{
			struct epoll_event ev;
			ev.events = events;
			ev.data.fd = sockfd;
			int n = epoll_ctl(_epfd,flag,sockfd,&ev);
			if(n < 0)
			{
				std::cout << "epoll_ctl error" << std::endl;
				return;
			}
			std::cout << "epoll_ctl success" << std::endl;
		}
		void ADD(int sockfd,uint32_t events) // 注册事件
		{
			Ctl(sockfd,events,EPOLL_CTL_ADD);
		}
		void Mod(int sockfd,uint32_t events)
		{
			Ctl(sockfd,events,EPOLL_CTL_MOD);
		}
		void Del(int sockfd)
		{
			Ctl(sockfd,0,EPOLL_CTL_DEL);
		}
		int Wait(struct epoll_event events[],int num,int timeout) // 等待事件发生
		{
			int n = epoll_wait(_epfd,events,num,timeout);
			if(n < 0)
			{				
				std::cout << "epoll_wait error" << std::endl;
				return -1;
			}
			std::cout << "epoll_wait success" << std::endl;
			return n;
		}
	
	private:
		int _epfd;
	};
}